package com.woniu.defecttracking.aop;/**
 * @Author :
 * @DateTime:2022/1/3 14:15
 * @discription: TODO
 **/

import com.woniu.defecttracking.annotation.WorkLog;
import com.woniu.defecttracking.annotation.WorkLogAuto;
import com.woniu.defecttracking.controller.bugs.WorkLogController;
import com.woniu.defecttracking.exception.WorkLogException;
import com.woniu.defecttracking.param.AddWorkLogParam;
import io.swagger.models.auth.In;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.CodeSignature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.xml.crypto.Data;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *@ClassName 类名： WorkLogAOP
 *@Description 描述：
 *@Author hasee
 *@Date 时间：2022/1/3 14:15
 *@Param 参数：
 *@Retrun 返回：
 *@version 1.0
 **/
@Aspect
@Component
public class WorkLogAdvice {

    @Autowired
    private WorkLogController workLogController;

    @Before("@annotation(com.woniu.defecttracking.annotation.WorkLog)")
    public void addWorkLog(JoinPoint joinPoint) throws WorkLogException {
        Object[] args = joinPoint.getArgs();
        System.out.println("args[0]==========================="+args[0]);
        //获取被注解的方法
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("method==========================="+method);
        //获取方法上的注解内容
        WorkLog annotation = method.getDeclaredAnnotation(WorkLog.class);
        System.out.println("annotation.bugId()====================="+annotation.bugId());

        //生成当前时间
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1 = simpleDateFormat.format(date);



        //生成新增日志所需要的参数对象
        AddWorkLogParam addWorkLogParam = AddWorkLogParam.builder().bugId(Integer.valueOf(annotation.bugId())).createPerson(annotation.createPerson())
                .createTime(annotation.createTime()).logDate(date1).logDesc(annotation.logDesc())
                .logState(annotation.logState()).modifyPerson(annotation.modifyPerson()).modifyTime(date1)
                .workHours(new BigDecimal(annotation.workHours())).build();
        workLogController.addWorkLog(addWorkLogParam);

    }

    @Before("@annotation(com.woniu.defecttracking.annotation.WorkLogAuto)")
    public void addWorkLogAuto(JoinPoint joinPoint) throws WorkLogException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Object[] args = joinPoint.getArgs();
        System.out.println("args[0]==========================="+args[0]);
        //获取被注解的方法
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println("method==========================="+method);
        //获取方法上的注解内容
        WorkLogAuto annotation = method.getDeclaredAnnotation(WorkLogAuto.class);
        System.out.println("annotation.bugId()====================="+annotation.bugId());
        Parameter[] parameters = method.getParameters();
        System.out.println("parameters====================="+parameters);

        for (Parameter parameter : parameters) {
            Class<?> type = parameter.getType();
            System.out.println(type);

            //获取bugid值
            Field field = type.getDeclaredField(annotation.bugId());
            String name = field.getName();
            System.out.println(name);
            String getter="get"+name.substring(0,1).toUpperCase()+name.substring(1);
            Method getMethod = args[0].getClass().getDeclaredMethod(getter);
            Integer bugId = Integer.valueOf(getMethod.invoke(args[0]).toString());
            System.out.println("bugId============"+bugId);

            //获取 createPerson 的值
            Field field2 = type.getDeclaredField(annotation.createPerson());
            String name2 = field2.getName();
            System.out.println(name2);
            String getter2="get"+name2.substring(0,1).toUpperCase()+name2.substring(1);
            Method getMethod2 = args[0].getClass().getDeclaredMethod(getter2);
            Integer createPerson = Integer.valueOf(getMethod2.invoke(args[0]).toString());
            System.out.println("createPerson============"+createPerson);

            //获取 createTime 的值
            Field field3 = type.getDeclaredField(annotation.createTime());
            String name3 = field3.getName();
            System.out.println(name3);
            String getter3="get"+name3.substring(0,1).toUpperCase()+name3.substring(1);
            Method getMethod3 = args[0].getClass().getDeclaredMethod(getter3);
            String createTime = getMethod3.invoke(args[0]).toString();
            System.out.println("createTime============"+createTime);

            //获取 logDesc 的值
            Field field4 = type.getDeclaredField(annotation.logDesc());
            String name4 = field4.getName();
            System.out.println(name4);
            String getter4="get"+name4.substring(0,1).toUpperCase()+name4.substring(1);
            Method getMethod4 = args[0].getClass().getDeclaredMethod(getter4);
            String logDesc = getMethod4.invoke(args[0]).toString();
            System.out.println("logDesc============"+logDesc);

            //获取 modifyPerson 的值
            Field field5 = type.getDeclaredField(annotation.modifyPerson());
            String name5 = field5.getName();
            System.out.println(name5);
            String getter5="get"+name5.substring(0,1).toUpperCase()+name5.substring(1);
            Method getMethod5 = args[0].getClass().getDeclaredMethod(getter5);
            Integer modifyPerson =Integer.valueOf( getMethod5.invoke(args[0]).toString());
            System.out.println("modifyPerson============"+modifyPerson);




            //获取 workHours 的值
            Field field6 = type.getDeclaredField(annotation.workHours());
            String name6 = field6.getName();
            System.out.println(name6);
            String getter6="get"+name6.substring(0,1).toUpperCase()+name6.substring(1);
            Method getMethod6 = args[0].getClass().getDeclaredMethod(getter6);
            String workHours = getMethod6.invoke(args[0]).toString();
            System.out.println("workHours============"+workHours);


            //生成当前时间
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date1 = simpleDateFormat.format(date);

            //生成新增日志所需要的参数对象
            AddWorkLogParam addWorkLogParam = AddWorkLogParam.builder().bugId(bugId).createPerson(createPerson.toString())
                    .createTime(createTime).logDate(date1).logDesc(logDesc)
                    .logState(annotation.logState()).modifyPerson(modifyPerson.toString()).modifyTime(date1)
                    .workHours(new BigDecimal(workHours)).build();




            //放入参数对象，生成日志记录
            workLogController.addWorkLog(addWorkLogParam);
        }

    }


}
